package cms

import (
	"wave-admin/global"
	"wave-admin/model/cms/request"
	"wave-admin/model/common/response"
	"wave-admin/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BlogApi struct{}

func (a *BlogApi) BlogConfig(ctx *gin.Context) {
	if config, err := blogService.BlogConfig(); err != nil {
		global.GnLog.Error("获得博客配置失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		//data, _ := json.Marshal(config)
		response.Success(ctx, config, "获得博客配置成功！")
	}
}

func (a *BlogApi) BlogConfigSave(ctx *gin.Context) {
	req := &request.ReqConfig{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err := blogService.BlogConfigSave(*req); err != nil {
		global.GnLog.Error("修改博客配置失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.SuccessWithMessage(ctx, "修改博客配置成功！")
	}
}

func (a *BlogApi) BlogUpload(ctx *gin.Context) {
	_, header, err1 := ctx.Request.FormFile("file")
	if err1 != nil {
		global.GnLog.Error("接收文件失败!", zap.Error(err1))
		response.FailWithMessage(ctx, "接收文件失败")
		return
	}
	url, _, err := blogService.UploadFile(header)
	if err != nil {
		global.GnLog.Error("上传失败!", zap.Error(err))
		response.FailWithMessage(ctx, "上传失败"+err.Error())
	} else {
		response.Success(ctx, url, "上传成功！")
	}
}
